package com.chandra.pos.customer.service.impl;

import com.chandra.pos.customer.dao.entity.*;
import com.chandra.pos.customer.model.*;
import com.chandra.pos.customer.service.CustomerRole;
import com.chandra.pos.customer.service.TransactionRole;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.persistence.EntityExistsException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 8/15/14.
 */
@Component
public class TransactionRoleImpl implements TransactionRole {


    @Autowired
    TransactionRepository transactionRepository;


    @Override
    public TransactionList getTransactionHistory(TransactionFilter transactionFilter) {

        TransactionList transactionList=new TransactionList();

        PageRequest request =
                new PageRequest(transactionFilter.getPage()-1 , transactionFilter.getRows(), transactionFilter.getDBSord(), transactionFilter.getSidx());

        Page<TransactionEntity> page=null;

         if(transactionFilter.getCustomerId()!=null && transactionFilter.getFromDate() !=null && transactionFilter.getToDate() !=null  && transactionFilter.getTransactionType() !=null){
             page= transactionRepository.search(transactionFilter.getCustomerId(),transactionFilter.getTransactionType(),transactionFilter.getFromDate(),transactionFilter.getToDate(), request);
        }else if(transactionFilter.getCustomerId()!=null && transactionFilter.getFromDate() !=null && transactionFilter.getToDate() !=null ){
             page= transactionRepository.search(transactionFilter.getCustomerId(),transactionFilter.getFromDate(),transactionFilter.getToDate(), request);
         }
        else if(transactionFilter.getCustomerId()!=null && transactionFilter.getTransactionType() !=null){
             page= transactionRepository.search(transactionFilter.getCustomerId(),transactionFilter.getTransactionType(), DateTime.now().withTimeAtStartOfDay(), DateTime.now(), request);
         }
        else if(transactionFilter.getCustomerId()!=null )

             page= transactionRepository.findByCustomerId(transactionFilter.getCustomerId(), request);
        else if( transactionFilter.getFromDate() !=null && transactionFilter.getToDate() !=null  && transactionFilter.getTransactionType() !=null){
             page= transactionRepository.search(transactionFilter.getTransactionType(),transactionFilter.getFromDate(),transactionFilter.getToDate(), request);
        }else if( transactionFilter.getFromDate() !=null && transactionFilter.getToDate() !=null ){
             page= transactionRepository.search(transactionFilter.getFromDate(),transactionFilter.getToDate(), request);
        }
        else if( transactionFilter.getTransactionType() !=null){
             page= transactionRepository.search(transactionFilter.getTransactionType(), DateTime.now().withTimeAtStartOfDay(), DateTime.now(), request);
        }else
             page= transactionRepository.search( DateTime.now().withTimeAtStartOfDay(), DateTime.now(), request);

        transactionList.setPage(transactionFilter.getPage());
        transactionList.setRecords(page.getTotalElements());
        transactionList.setTotal(page.getTotalPages());
        transactionList.setTransactions(page.getContent());

        Double totalTransactionAmount=0d;


        for(TransactionEntity transactionEntity:transactionList.getTransactions()){
            totalTransactionAmount=totalTransactionAmount+transactionEntity.getTransactionAmount();

        }

        transactionList.setTotalTransactionAmount(totalTransactionAmount);
        return transactionList;
    }

    @Override
    public TransactionEntity saveTransaction(TransactionEntity transactionEntity) {
        return transactionRepository.save(transactionEntity);
    }


}
